<?php
/**
 * @notes:
 * @return
 * @author: admin
 * @date: 2024/9/11
 */

namespace addons\shopro\controller\tokens;

use addons\shopro\controller\Common;
use addons\shopro\service\tokens\TradeService;

class Trade extends Common
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    /***
     * 交易中心列表
     * @notes:
     * @throws \think\exception\DbException
     * @author: 小谢
     * @date: 2024/9/11 13:49
     */
    public function index()
    {
        $type = $this->request->param('type', 'buy');
        if (!in_array($type, ['buy', 'sell', 'my'])) {
            $this->error("参数错误");
        }
        $user = auth_user();
        $this->success('获取成功',(new TradeService())->getList($user,$type));
    }

    /***
     * 获取行情数据
     * @notes:
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: 小谢
     * @date: 2024/9/11 19:47
     */
    public function getMarketInfo()
    {

        $this->success('获取成功',(new TradeService())->getMarketInfo());
    }

    /***
     * 交易处理
     * @notes:
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: 小谢
     * @date: 2024/9/11 16:43
     */
    public function submit()
    {
        if(sheep_config('shop.basic.transaction')=='0')
        {
            $this->error('操作不在时间范围内');
        }
        $param=$this->request->only(['order_num','rep_password']);
        $user = auth_user();
        if ($user['order_switch']) {
            $this->error("账号异常，请稍后重试");
        }
        if($this->svalidate($param,'.submit')){
            if(!$user['wit_switch']){
                $this->error('您的账号异常');
            }
            if ($user['lock_status'] == 1) {
                $this->error('账号异常');
            }
            $res=(new TradeService())->tradeSubmit($user,$param);
            if($res==='ok'){
                $this->success('交易成功！');
            }
            $this->error($res);
        }

    }

    /***
     * 关闭订单
     * @notes:
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: 小谢
     * @date: 2024/9/11 16:57
     */
    public function close()
    {
        $param=$this->request->only(['order_num','rep_password']);

        if($this->svalidate($param,'.submit')){
            $user = auth_user();
            $res=(new TradeService())->closeSubmit($user,$param);
            if($res==='ok'){
                $this->success('取消成功！');
            }
            $this->error($res);
        }
    }

    /***
     * 发布求购
     * @notes:
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @author: 小谢
     * @date: 2024/9/11 18:19
     */
    public function open()
    {
        if(sheep_config('shop.basic.transaction',false)=='0')
        {
            $this->error('操作不在时间范围内');
        }

        $param=$this->request->only(['number','price','rep_password','type', 'walletType']);
        if (!in_array($param['type'], ['buy', 'sell'])) {
            $this->error("参数错误");
        }

        $user = auth_user();

        if ($user['order_switch']) {
            $this->error("账号异常，请稍后重试");
        }

        if($this->svalidate($param,'.open')){

            if(!$user['wit_switch']){
                $this->error('您的账号异常');
            }
            if ($user['lock_status'] == 1) {
                $this->error('账号异常');
            }
            $res=(new TradeService())->openSubmit($user,$param);
            if($res==='ok'){
                $this->success('发布成功！');
            }
            $this->error($res);
        }
    }

}